package com.suixinsanren.design.observer.demo02;

import java.util.ArrayList;

public class WeatherData implements Subject {

    private String date;
    private String maxTemperature;
    private String minTemperature;
    private String remark;
    private ArrayList<Observer> observers;
    private boolean isChange = false;

    public WeatherData() {
        this.observers = new ArrayList<>();
    }

    public void update(String date, String maxTemperature, String minTemperature, String remark) {
        this.date = date;
        this.maxTemperature = maxTemperature;
        this.minTemperature = minTemperature;
        this.remark = remark;
        setChanged();
        notifyObservers();
    }


    @Override
    public void addObserver(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        observers.remove(observer);
    }

    @Override
    public void notifyObservers() {
        if (isChange) {
            observers.forEach(item -> item.update(this.date, this.maxTemperature, this.minTemperature, this.remark));
        }
    }

    @Override
    public void setChanged() {
        this.isChange = true;
    }
}
